package com.foreknow.controller;

import com.foreknow.bean.GuestBook;
import com.foreknow.service.GuestBookService;
import com.foreknow.service.GuestBookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;


public class GetMessageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        //调用service的方法,查询所有留言的数据
        GuestBookService service = new GuestBookServiceImpl();
        List<Object>list = service.showAllBook();

        //如何将list集合保存到一个地方,转发之后就可以在list.jsp中方便获取,需要将list保存到服务器端
        req.setAttribute("list",list);

        //当前的Servlet中如何跳转到jsp页面
        req.getRequestDispatcher("/list.jsp").forward(req,resp);

//        out.println("<html>");
//        out.println("<head>");
//        out.println("<title>servlet</title>");
//        out.println("</head>");
//        out.println("<body>");
//        out.println("<table>");
//        out.println("<tr>");
//        out.println("<td>id</td>");
//        out.println("<td>name</td>");
//        out.println("<td>phone</td>");
//        out.println("<td>email</td>");
//        out.println("<td>title</td>");
//        out.println("<td>content</td>");
//        out.println("<td>createdtime</td>");
//        out.println("</tr>");
//        for (int i = 0; i < list.size(); i++) {
//            GuestBook gb = (GuestBook) list.get(i);
//            out.println("<tr>");
//            out.println("<td>"+gb.getID()+"</td>");
//            out.println("<td>"+gb.getName()+"</td>");
//            out.println("<td>"+gb.getPhone()+"</td>");
//            out.println("<td>"+gb.getEmail()+"</td>");
//            out.println("<td>"+gb.getTitle()+"</td>");
//            out.println("<td>"+gb.getContent()+"</td>");
//            out.println("<td>"+gb.getCreatedtime()+"</td>");
//            out.println("</tr>");
//        }
//        out.println("</table>");
//        out.println("</body>");
//        out.println("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
